<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定时嵌套</title>
</head>
<style>
    div{
        width: 100px;
        height: 100px;
        background-color: yellowgreen;
        position: absolute;
    }
</style>
<body>
    <div></div>
</body>
</html>

<script>
    //下面是一个定时器执行结束后，执行另外一个定时器
    function interval(callback,delay=100){
        let id = setInterval(() => callback(id),delay);
    }

    const div = document.querySelector("div");
    interval((timeId)=>{
        const left = parseInt(window.getComputedStyle(div).left);
        div.style.left = left + 10 + 'px';
        if(left >= 500){
            clearInterval(timeId);
            interval((timeIds) => {
            const width = parseInt(window.getComputedStyle(div).width);
            div.style.width = (width - 10) + 'px';
            if(width <= 0){
                clearInterval(timeIds);
            }
        },50);
        }
        
    },50);

</script>